import { IPSDEWizardPanel } from '@ibizstudio/runtime';
import { ControlServiceBase } from '@ibizstudio/runtime';
import { GlobalService } from '@ibizstudio/runtime';
import { AppWizardPanelModel } from '../ctrl-model';

export class AppWizardPanelService extends ControlServiceBase {
  /**
   * 表格实例对象
   *
   * @memberof AppGridService
   */
  declare controlInstance: IPSDEWizardPanel;

  /**
   * 数据服务对象
   *
   * @type {any}
   * @memberof AppGridService
   */
  appEntityService!: any;

  /**
   * 初始化服务参数
   *
   * @type {boolean}
   * @memberof AppGridService
   */
  async initServiceParam() {
    this.appEntityService = await new GlobalService().getService(this.appDeCodeName, this.context);
    this.model = new AppWizardPanelModel(this.controlInstance);
  }

  /**
   * Creates an instance of AppGridService.
   *
   * @param {*} [opts={}]
   * @memberof AppGridService
   */
  constructor(opts: any = {}, context?: any) {
    super(opts, context);
    this.controlInstance = opts;
  }

  async loaded() {
    await this.initServiceParam();
  }

  /**
   * 初始化向导
   *
   * @param {string} action
   * @param {*} [context={}]
   * @param {*} [data={}]
   * @param {boolean} [isloading]
   * @returns {Promise<any>}
   * @memberof ${srfclassname('${ctrl.codeName}')}Service
   */
  init(action: string, context: any = {}, data: any = {}, isloading?: boolean): Promise<any> {
    const { data: Data, context: Context } = this.handleRequestData(action, context, data);
    return new Promise((resolve: any, reject: any) => {
      let result: Promise<any>;
      const _appEntityService: any = this.appEntityService;
      if (_appEntityService[action] && _appEntityService[action] instanceof Function) {
        Object.assign(Data, data.viewparams);
        result = _appEntityService[action](Context, Data, isloading);
      } else {
        result = this.appEntityService.Create(Context, Data, isloading);
      }
      result
        .then(response => {
          this.handleResponse(action, response);
          resolve(response);
        })
        .catch(response => {
          reject(response);
        });
    });
  }

  /**
   * 向导结束
   *
   * @param {string} action
   * @param {*} [context={}]
   * @param {*} [data={}]
   * @param {boolean} [isloading]
   * @returns {Promise<any>}
   * @memberof ${srfclassname('${ctrl.codeName}')}Service
   */
  finish(action: string, context: any = {}, data: any = {}, isloading?: boolean): Promise<any> {
    const { data: Data, context: Context } = this.handleRequestData(action, context, data);
    return new Promise((resolve: any, reject: any) => {
      let result: Promise<any>;
      const _appEntityService: any = this.appEntityService;
      // 忽略版本检查
      Object.assign(Data, { ignoreversioncheck: 1 });
      if (_appEntityService[action] && _appEntityService[action] instanceof Function) {
        result = _appEntityService[action](Context, Data, isloading);
      } else {
        result = this.appEntityService.Update(Context, Data, isloading);
      }
      result
        .then(response => {
          this.handleResponse(action, response);
          resolve(response);
        })
        .catch(response => {
          reject(response);
        });
    });
  }
}
